package main

import (
	"fmt"
	"github.com/kataras/iris"
	"github.com/kataras/iris/mvc"
	"math/rand"
	"strings"
	"sync"
	"time"
)

type lotteryController struct {
	Ctx iris.Context
}

var (
	userList []string
	mu       sync.Mutex
)

func newApp() (app *iris.Application) {
	app = iris.New()
	mvc.New(app.Party("/")).Handle(&lotteryController{})
	return app
}

func main() {
	var (
		app *iris.Application
	)

	app = newApp()

	//userList = make([]string, 0)
	userList = []string{}
	mu = sync.Mutex{}

	_ = app.Run(iris.Addr(":8080"))
}

func (c *lotteryController) Get() string {
	var (
		count int
	)

	count = len(userList)

	return fmt.Sprintf("当前总共参与抽奖的用户数：%d\n", count)
}

// 导入抽奖用户
// POST http://127.0.0.1:8080/import
// params: users
func (c *lotteryController) PostImport() string {

	var (
		strUsers string
		count1   int
		count2   int
		u        string
	)

	strUsers = c.Ctx.FormValue("users")

	users := strings.Split(strUsers, ",")

	mu.Lock()
	defer mu.Unlock()

	count1 = len(userList)

	for _, u = range users {
		u = strings.TrimSpace(u)
		if len(u) > 0 {
			userList = append(userList, u)
		}
	}

	count2 = len(userList)

	return fmt.Sprintf("当前总共参与抽奖的用户数： %d，成功导入的用户数：%d\n", count2, count2-count1)
}

// 抽奖
// GET http://127.0.0.1:8080/lucky
func (c *lotteryController) GetLucky() string {
	mu.Lock()
	defer mu.Unlock()
	count := len(userList)
	if count > 0 {
		seed := time.Now().UnixNano()
		index := rand.New(rand.NewSource(seed)).Int31n(int32(count))
		user := userList[index]
		userList = append(userList[0:index], userList[index+1:]...)
		return fmt.Sprintf("当前中奖用户： %s，剩余用户数：%d\n", user, count-1)
	} else if count == 1 {
		user := userList[0]
		return fmt.Sprintf("当前中奖用户： %s，剩余用户数：%d\n", user, count-1)
	} else {
		return fmt.Sprintf("已经没有参与用户，请先通过 /import 导入用户\n")
	}
}
